#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

deque<int> ready;
queue<int> block;
int n, quantum, c[5], var[128], ip[N];
bool locked;
char prog[N][10];

void run(int pid) {
  int q = quantum;
  while (q > 0) {
    char *p = prog[ip[pid]];
    switch (p[2]) {
      case '=':
        var[p[0]] = p[4] - '0';
        if (isdigit(p[5])) var[p[0]] = var[p[0]] * 10 + p[5] - '0';
        q -= c[0];
        break;
      case 'i':
        printf("%d: %d\n", pid + 1, var[p[6]]);
        q -= c[1];
        break;
      case 'c':
        if (locked) {
          block.push(pid);
          return;
        }
        locked = true;
        q -= c[2];
        break;
      case 'l':
        locked = false;
        if (block.size()) {
          ready.push_front(block.front());
          block.pop();
        }
        q -= c[3];
        break;
      case 'd':
        return;
    }
    ip[pid]++;
  }
  ready.push_back(pid);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d %d %d %d %d %d\n", &n, &c[0], &c[1], &c[2], &c[3], &c[4],
          &quantum);
    memset(var, 0, sizeof(var));
    locked = false;

    int line = 0;
    rep(i, 0, n - 1) {
      ip[i] = line;
      do {
        fgets(prog[line++], N, stdin);
      } while (prog[line - 1][2] != 'd');
      ready.push_back(i);
    }

    while (ready.size()) {
      int pid = ready.front();
      ready.pop_front();
      run(pid);
    }
    if (t) printf("\n");
  }
  return 0;
}